//
//  HappinessViewController.swift
//  Happiness
//
//  Created by sdw on 15/11/3.
//  Copyright (c) 2015年 ai.com. All rights reserved.
//

import UIKit

class HappinessViewController: UIViewController ,FaceViewDataSource{
    
    
    @IBOutlet weak var faceView: FaceView!
    {
        didSet{
            faceView.dataSoure = self
            faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:"))
        }
    }
    @IBOutlet weak var didPanGesture: UIPanGestureRecognizer!
    
    var happiness:Int = 75 {
        didSet{
            happiness = min(max(happiness,0),100)
            updateUI();
        }
    }
    private struct Constants{
        static let HappniessGestureScale :CGFloat = 4
    }
    
    @IBAction func dealPanGeture(gesture: UIPanGestureRecognizer) {
        switch gesture.state{
            case .Ended :fallthrough
            case .Changed :
                  let translation = gesture.translationInView(faceView)
                  let happniessChange = -Int(translation.y / Constants.HappniessGestureScale)
                  if happniessChange != 0{
                    happiness += happniessChange
                    gesture.setTranslation(CGPointZero, inView: faceView)
                  }
        
            default :break
        }
    }
   
    func updateUI() {
        faceView?.setNeedsDisplay()
        title = "\(happiness)"
    }
    
    func smilenessForFaceView( sender : FaceView)->Double?{
        return Double(happiness-50)/50
    }

}
